package com.aplink.generic.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.aplink.generic.database.DatabaseManager;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.xmlparser.MasterConfigParser;

public abstract class DataProvider extends ContentProvider {
    public static final String CLEAR_SYNC = "clearSync";
    public static final int CLEAR_SYNC_STATUS = 3;

    public static final int GET_ALL = 1;
    public static final int GET_DISTINCT = 2;
    public static final String TABLE = "table";
    private DatabaseManager mDatabaseManager;
    private MasterConfig mMasterConfig;
    private final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    @Override
    public int delete(final Uri uri, final String selection,
            final String[] selectionArgs) {
        if (this.sUriMatcher.match(uri) == DataProvider.CLEAR_SYNC_STATUS) {
            final String table = uri.getLastPathSegment();
            final SQLiteDatabase db = this.mDatabaseManager
                    .getWritableDatabase();
            return db.delete(table, selection, null);
        }
        return 0;
    }

    @Override
    public String getType(final Uri uri) {
        return null;
    }

    protected abstract int initMasterConfig();

    @Override
    public Uri insert(final Uri uri, final ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {
        this.mMasterConfig = MasterConfigParser.parserIntoControls(
                initMasterConfig(), getContext());
        if (this.mMasterConfig.getAutoSync() != null) {
            final String authority = this.mMasterConfig.getAutoSync()
                    .getAuthorities();
            if (!TextUtils.isEmpty(authority)) {
                this.sUriMatcher.addURI(authority, DataProvider.TABLE + "/*",
                        DataProvider.GET_ALL);
                this.sUriMatcher.addURI(authority, DataProvider.TABLE + "/*/#",
                        DataProvider.GET_DISTINCT);
                this.sUriMatcher.addURI(authority, DataProvider.CLEAR_SYNC
                        + "/*", DataProvider.CLEAR_SYNC_STATUS);
            }
        }
        this.mDatabaseManager = new DatabaseManager(getContext(),
                this.mMasterConfig);
        return true;
    }

    @Override
    public Cursor query(final Uri uri, final String[] projection,
            final String selection, final String[] selectionArgs,
            final String sortOrder) {
        if (this.sUriMatcher.match(uri) == DataProvider.GET_ALL) {
            final String table = uri.getLastPathSegment();
            final String queryString = SQLiteQueryBuilder.buildQueryString(
                    true, table, projection, selection, null, null, null, null);
            final SQLiteDatabase db = this.mDatabaseManager
                    .getReadableDatabase();
            return db.rawQuery(queryString, null);
        } else if (this.sUriMatcher.match(uri) == DataProvider.GET_DISTINCT) {
            final String table = uri.getPathSegments().get(1);
            final String queryString = SQLiteQueryBuilder.buildQueryString(
                    true, table, projection, selection, null, null, null, null);
            final SQLiteDatabase db = this.mDatabaseManager
                    .getReadableDatabase();
            return db.rawQuery(queryString, null);
        }
        return null;
    }

    @Override
    public int update(final Uri uri, final ContentValues values,
            final String selection, final String[] selectionArgs) {
        if (this.sUriMatcher.match(uri) == DataProvider.CLEAR_SYNC_STATUS) {
            final String table = uri.getLastPathSegment();
            final SQLiteDatabase db = this.mDatabaseManager
                    .getWritableDatabase();
            return db.update(table, values, selection, selectionArgs);
        }
        return 0;
    }

}
